package com.sanjiahantang.youjiarole.module.article.views.impl;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.article.presenters.ArticlePresenter;
import com.sanjiahantang.youjiarole.module.article.views.ArticleView;
import com.sanjiahantang.youjiarole.module.common.beans.HelpBean;
import com.sanjiahantang.youjiarole.module.common.views.impl.SearchActivity;
import com.sanjiahantang.youjiarole.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.sanjiahantang.youjiarole.utils.RelativeDateFormat;
import com.squareup.picasso.Picasso;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zy on 2017/10/31.
 */

public class ArticleListActivity extends BaseMvpActivity<ArticleView,ArticlePresenter> implements ArticleView,View.OnClickListener,BaseView.OnLoadListener {

    private ListView listView;
    private QuickAdapter<HelpBean.Item> listAdapter;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public ArticlePresenter initPresenter() {
        return new ArticlePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_article_list);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.cetSearch).setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);
        listView.setDivider(null);


        listAdapter = new QuickAdapter<HelpBean.Item>($this, R.layout.item_article) {
            @Override
            protected void convert(BaseAdapterHelper helper, final HelpBean.Item item) {
                helper.setText(R.id.txtTitle,item.getArTitle());
                ImageView ivIcon = helper.getView(R.id.ivIcon);
                if (!TextUtils.isEmpty(item.getUrl())){
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl())).into(ivIcon);
                }

                try {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
                    Date date = df.parse(item.getCreateTime());
                    helper.setText(R.id.txtTime, RelativeDateFormat.format(date));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, WebActivity.class);
                        intent.putExtra(KEY_ID,item.getArticleNo());
                        intent.putExtra(KEY_TYPE,WEB_TYPE_MATCH_CENTER);
                        context.startActivity(intent);
                    }
                });
            }
        };

        listView.setAdapter(listAdapter);
    }

    @Override
    public void initData() {
        int id = getIntent().getIntExtra(KEY_ID,0);
        Map<String,Object> map = new HashMap<>();
        if (id != 0){
            map.put("arTypeId",id);
        }
        presenter.searchArTrainInfoType($this,map,this,2);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.cetSearch:
                Intent intent = new Intent($this, SearchActivity.class);
                intent.putExtra(KEY_TYPE,SEARCH_TYPE_ARTICLE);
                startActivityForResult(intent,INTENT_SEARCH);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case INTENT_SEARCH:
                    presenter.searchArTrainInfoRole($this,data.getStringExtra(KEY_KEYWORDS),this);
                    break;
            }
        }
    }

    @Override
    public void onLoad(Object content) {
        listAdapter.clear();
        listAdapter.addAll((List<HelpBean.Item>) content);
    }
}
